package com.design.strategy;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lijiaming
 * @version 1.0
 * @description Excel文件处理工厂
 * @date 2025/9/26 11:13
 */
@Component
public class ParseChannelFileHandlerFactory implements ApplicationContextAware, InitializingBean {

    private Map<String, ParseChannelFileHandler> channelMap = new HashMap<>();

    private ApplicationContext context;

    public ParseChannelFileHandler getParseHandleService(String channelSource) {
        return channelMap.get(channelSource);
    }

    public Map<String, ParseChannelFileHandler> getParseHandleServiceMap() {
        return channelMap;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        Map<String, ParseChannelFileHandler> serviceList = context.getBeansOfType(ParseChannelFileHandler.class);
        for (Map.Entry<String, ParseChannelFileHandler> entry : serviceList.entrySet()) {
            channelMap.put(entry.getValue().getChannelSource(), entry.getValue());
        }
    }
}
